<template>
  <div class="app-container">
    <div style="background-color: white;padding: 10px 10px 10px 10px">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="检查名称" prop="checkName" label-width="120">
          <el-input
            v-model="queryParams.checkName"
            placeholder="请输入检查名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="background-color: white;margin-top: 10px;padding: 20px">
      <el-row :gutter="10" class="mb8">
        <!--        <el-col :span="1.5">
                  <el-button
                    type="primary"
                    icon="el-icon-folder-remove"
                    size="mini"
                    @click="handleTemplate"
                    v-hasPermi="['patrol:check:add']"
                  >模板管理
                  </el-button>
                </el-col>-->
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['patrol:check:add']"
          >新增
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="checkList" @selection-change="handleSelectionChange"
                :header-cell-style="{background:'#E3E3E3'}">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="序号" align="center" type="index"/>
        <el-table-column label="检查名称" align="center" prop="checkName"/>
        <el-table-column label="填报日期范围" align="center" prop="schoolName">
          <template v-slot="scope">
            <span>{{ scope.row.answerStartDate }}——{{ scope.row.answerEndDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="填报情况" align="center" prop="status">
          <template v-slot="scope">
            <span style="color:#07A387 ">{{ scope.row.yesFilled }}已填</span>/
            <span style="color:#E94242 ">{{ scope.row.notFilled }}未填</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template v-slot="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template v-slot="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleAnswer(scope.row)"
              v-hasPermi="['patrol:check:edit']"
            >查看
            </el-button>
<!--            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['patrol:check:edit']"
            >修改
            </el-button>-->
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['patrol:check:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改专项检查对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="检查名称" prop="cId">
            <el-select v-model="form.cId" placeholder="请选择检查类型" clearable filterable :style="{width:'100%'}">
              <el-option v-for="(item,index) in checkTypeData" :key="index" :label="item.checkName"
                         :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="学校名称" prop="schoolName">
            <el-autocomplete :style="{width:'100%'}"
                             v-model="form.schoolName"
                             :fetch-suggestions="querySearch"
                             placeholder="请输入学校名称"
                             @select="handleSelectSchool"
            ></el-autocomplete>
          </el-form-item>
          <el-form-item label="填报日期范围" prop="dateScope">
            <el-date-picker
              v-model="form.dateScope"
              type="daterange"
              align="center"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {listCheck, getCheck, delCheck, addCheck, updateCheck, exportCheck} from "@/api/patrol/check";
import {
  listCheckType,
  getCheckType
} from '@/api/patrol/checkType'

export default {
  name: "Check",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专项检查表格数据
      checkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkName: null,
        schoolName: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cId: [
          {required: true, message: "检查名称不能为空", trigger: "blur"}
        ],
        schoolName: [
          {required: true, message: "学校名称不能为空", trigger: "blur"}
        ],
        dateScope: [
          {required: true, message: '填报日期范围不能为空', trigger: 'blur'}
        ],
      },
      checkTypeData: [],
      schoolData: [],
      dateScope: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询专项检查列表 */
    getList() {
      this.loading = true;
      listCheckType(this.queryParams).then(response => {
        console.log(response)
        this.checkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cId: null,
        schoolId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    querySearch(queryString, cb) {
      let a;
      if (queryString) {
        a = this.schoolData.filter(item => item.value.includes(queryString))
      } else {
        a = this.schoolData
      }
      cb(a)
    },
    handleSelectSchool(item) {
      this.form.schoolId = item.schoolId
    },
    /**
     * 模板管理
     */
    handleTemplate() {
      this.$router.push({
        path: '/form/template',
      })
    },
    /**
     * 查看检查详情
     */
    handleAnswer(data) {
      this.$router.push({
        path: '/form/viewCheck',
        query: {
          checkId: encodeURI(JSON.stringify(data.id)),
          checkName: encodeURI(JSON.stringify(data.checkName))
        }
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        path: '/form/addCheck'
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCheckType(id).then(response => {
        this.form = response.data;
        console.log(this.form);
        this.open = true;
        this.title = "修改专项检查";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.answerEndDate = this.dateScope[1]
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCheck(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheck(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除专项检查编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delCheck(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有专项检查数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportCheck(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
<style scoped>
.app-container {
  background-color: #edf1f2;
  padding: 10px;
  min-height: calc(100vh - 35px)
}
</style>
